
export default store => {
  // 程序启动读取一次配置
  try {
    // 此处开始读取数据
    // var res = JSON.parse(window.sessionStorage.state)
    // store.replaceState(res)
    // console.log('取出数据……')
    // console.log(res)

    if (window.sessionStorage.eafWX_App) {
      store.state.app = JSON.parse(window.sessionStorage.eafWX_App)
    }
    if (window.sessionStorage.eafWX_ErrorLog) {
      store.state.errorLog = JSON.parse(window.sessionStorage.eafWX_ErrorLog)
    }
    if (window.sessionStorage.eafWX_Permission) {
      store.state.permission = JSON.parse(window.sessionStorage.eafWX_Permission)
    }
    if (window.sessionStorage.eafWX_Settings) {
      store.state.settings = JSON.parse(window.sessionStorage.eafWX_Settings)
    }
    if (window.sessionStorage.eafWX_TagsView) {
      store.state.tagsView = JSON.parse(window.sessionStorage.eafWX_TagsView)
    }
    if (window.sessionStorage.eafWX_User) {
      store.state.user = JSON.parse(window.sessionStorage.eafWX_User)
    }
  } catch (error) {
    console.log('解析json字符串失败' + error)
    console.log(window.sessionStorage.state)
  }

  // 每次commit出发保存
  store.subscribe((mutation, state) => {
    try {
      // // 解决json序列化循环引用
      // var cache = []
      // var res = JSON.stringify(state, function(key, value) {
      //   if (typeof value === 'object' && value !== null) {
      //     if (cache.indexOf(value) !== -1) {
      //       return
      //     }
      //     cache.push(value)
      //   }
      //   return value
      // })
      // cache = null

      // console.log('保存数据……')
      // console.log(state)
      // var res = JSON.stringify(state)
      // window.sessionStorage.state = res

      if (mutation.type.indexOf('app') === 0) {
        window.sessionStorage.eafWX_App = JSON.stringify(state.app)
      } else if (mutation.type.indexOf('errorLog') === 0) {
        window.sessionStorage.eafWX_ErrorLog = JSON.stringify(state.errorLog)
      } else if (mutation.type.indexOf('permission') === 0) {
        window.sessionStorage.eafWX_Permission = JSON.stringify(state.permission)
      } else if (mutation.type.indexOf('settings') === 0) {
        window.sessionStorage.eafWX_Settings = JSON.stringify(state.settings)
      } else if (mutation.type.indexOf('tagsView') === 0) {
        window.sessionStorage.eafWX_TagsView = JSON.stringify(state.tagsView)
      } else if (mutation.type.indexOf('user') === 0) {
        window.sessionStorage.eafWX_User = JSON.stringify(state.user)
      }
    } catch (error) {
      console.log(state)
      console.log('保存数据出错，主要对象中不要有属性引用对象自身 mutations方法：' + mutation.type + ' 信息：' + error)
    }
  })
}
